package com.kmzx.one.project.controller;

import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.kmzx.one.project.others.dto.WikiH5PayCallback;
import com.kmzx.one.project.service.ZOutService;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.naming.NoNameCoder;
import com.thoughtworks.xstream.io.xml.Xpp3Driver;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@RestController
public class ZOutController extends BaseController {


    @Autowired
    ZOutService zOutService;


    @ApiOperation(value = "1.微信H5支付_req")
    @PostMapping(value = "mpay/gen")
    public Map<String, Object> dealRobotOrder(HttpServletRequest request) {
        try {
            String clientIP = request.getAttribute("clientIP").toString();
            InputStream inputStream = request.getInputStream();
            String postData = IOUtils.toString(inputStream, "UTF-8");
            logger.info("微信H5支付,API接口数据:{}", postData);
            Map<String, Object> objectMap = zOutService.receiveReq(postData, clientIP);

            return objectMap;
        } catch (IOException e) {
            e.printStackTrace();
            Map<String, Object> hashMap = new HashMap<>();
            hashMap.put("code", "400");
            hashMap.put("msg", "参数错误");
            return hashMap;
        }

    }


    @ApiOperation(value = "2.微信H5支付异步通知")
    @RequestMapping(value = "/mpay/callback/{orderNo}", method = {RequestMethod.POST, RequestMethod.GET})
    public void wikiH5CallBack(HttpServletRequest request, HttpServletResponse response, @PathVariable("orderNo") String orderNo) throws Exception {
        InputStream inputStream = request.getInputStream();
        String postData = IOUtils.toString(inputStream, "UTF-8");
        logger.info("微信H5支付,请求回调,回调数据为:" + postData);

        try {
            XStream xStream = new XStream(new Xpp3Driver(new NoNameCoder()));
            xStream.alias("xml", WikiH5PayCallback.class);
            WikiH5PayCallback wikiH5PayCallback = (WikiH5PayCallback) xStream.fromXML(postData);
            PrintWriter out = response.getWriter();
            String result_code = wikiH5PayCallback.getResult_code();
            String out_trade_no = wikiH5PayCallback.getOut_trade_no();
            zOutService.resolveOutOrder(out_trade_no,result_code,postData);
            String oks = WxPayNotifyResponse.success("OK");
            String nos = WxPayNotifyResponse.fail("参数校验错误");
            out.print(oks);
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
